<?php
/**
 *
 *
 */
namespace Yz\modules\Generic;

class Controller_Add
    extends Controller_Edit
{


    /** allow presence in automatic menus
     *  @const ???
     */
    const IN_MENU = true;

    /******************************
     *     Protected methods      *
     ******************************/


    /** parse arguments: determine item-id
     *  @param Yz_Array args (from URI)
     *  @return this
     *  @throw Yz_Controller_Exception_InvalidArgument
     */
    protected function parseArguments(\Yz_Array $args)
    {
        return $this;
    }

    /**
     *  @return item
     */
    protected function fetchItem()
    {
        debug();
        return $this->selectModel()->newItem();
    }



    /**
     *  @return this
     */
    protected function updateModel()
    {
        if (null === $this->error) {
            try {
                $this->model->addItem($this->item);
                $this->success = true;
            } catch (\Yz_SQL_Exception_Unique $ex) {
                $this->error = $ex;
            }
        }
        return $this;
    }



    /** parent getErrorMessage + unique exception message
     *  @return string
     */
    protected function getErrorMessage()
    {
        $msg = parent::getErrorMessage();
        if ($this->error instanceof \Yz_SQL_Exception_Unique) {
            $msg .= ':Unique';
        }
        return $msg;
    }

}
